package com.sinosoft.ptas.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;

import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.ptas.dao.AnnualTargetMapper;
import com.sinosoft.ptas.dao.IndexMapper;
import com.sinosoft.ptas.dao.ReportEntityMapper;
import com.sinosoft.ptas.dao.TargetsOfImplPhaseMapper;
import com.sinosoft.ptas.entity.AnnualTargetEntity;
import com.sinosoft.ptas.entity.IndexEntity;
import com.sinosoft.ptas.entity.ReportEntity;
import com.sinosoft.ptas.entity.TargetsOfImplPhaseEntity;
import com.sinosoft.ptas.service.TargetsOfImplPhaseService;
import com.sinosoft.ptas.system.commom.AuditConstant;
import com.sinosoft.ptas.system.commom.ExceptionConstant;
import com.sinosoft.ptas.system.commom.PtasConstant;

/**
 * 实施期目标相关接口实现
 * @author lianWuJun
 * @time   2017年2月18日 下午6:48:43
 */
@Validated
public class TargetsOfImplPhaseServiceImpl implements TargetsOfImplPhaseService{

	TargetsOfImplPhaseMapper targetsOfImplPhaseMapper;
	AnnualTargetMapper annualTargetMapper;
	IndexMapper indexMapper;
	ReportEntityMapper reportMapper;
	
	
	
	public void setReportMapper(ReportEntityMapper reportMapper) {
		this.reportMapper = reportMapper;
	}

	public void setAnnualTargetMapper(AnnualTargetMapper annualTargetMapper) {
		this.annualTargetMapper = annualTargetMapper;
	}
	
	public void setIndexMapper(IndexMapper indexMapper) {
		this.indexMapper = indexMapper;
	}

	public void setTargetsOfImplPhaseMapper(
			TargetsOfImplPhaseMapper targetsOfImplPhaseMapper) {
		this.targetsOfImplPhaseMapper = targetsOfImplPhaseMapper;
	}

	

	/**
	 * 创建实施期目标
	 * 创建实施期目标成功之后，继续批量添加指标，批量添加年度目标
	 * @param targetsOfImplPhaseEntity
	 * @return 实施期目标的编号
	 */
	public String createTargetsOfImplPhase( TargetsOfImplPhaseEntity targetsOfImplPhaseEntity) throws BusinessException{
		if(targetsOfImplPhaseEntity==null){
			throw new  BusinessException(ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE);
		}
		String id="";
			ReportEntity report= reportMapper.retrieveReportById(targetsOfImplPhaseEntity.getReportId());
			if(report!=null){
				if(report.getProjectStatus().equalsIgnoreCase(AuditConstant.AUDIT_SAVE)){//保存状态
					List<TargetsOfImplPhaseEntity> targs = targetsOfImplPhaseMapper.retrieveByReportIdForSort(targetsOfImplPhaseEntity.getReportId());
					id=UUID.randomUUID().toString();//因为实体类中没有编号，后台自动生成一个id
					targetsOfImplPhaseEntity.setId(id);
					targetsOfImplPhaseEntity.setSourceFlag(PtasConstant.ptas_source_flag);//设置默认值 0：代表默认目标 1：代表自定义实施期目标
					targetsOfImplPhaseEntity.setSort(targs.get(0).getSort()+1);//设置排序问题，给新生成实施期目标的sort设置值
					targetsOfImplPhaseMapper.createTargetsOfImplPhase(targetsOfImplPhaseEntity);
					List<AnnualTargetEntity> annualTargetEntitys=targetsOfImplPhaseEntity.getAnnualTargetDatalist();
					List<AnnualTargetEntity> anns=new ArrayList<AnnualTargetEntity>();
					List<IndexEntity> indexs=new ArrayList<IndexEntity>();
					if(annualTargetEntitys!=null&&annualTargetEntitys.size()>0){
						for(int i=0;i<annualTargetEntitys.size();i++){
							AnnualTargetEntity annT = annualTargetEntitys.get(i);
							annT.setId(UUID.randomUUID().toString());
							annT.setTargetsOfImplPhase(id);
							anns.add(annT);
						}
						annualTargetMapper.createAnnualTargets(anns);//批量创建年度目标
					}
					List<IndexEntity> indexEntitys=targetsOfImplPhaseEntity.getIndexEntityList();
					if(indexEntitys!=null&&indexEntitys.size()>0){
						for(int i=0;i<indexEntitys.size();i++){
							IndexEntity index = indexEntitys.get(i);
							index.setId(UUID.randomUUID().toString());
							index.setTargetsOfImplPhase(id);
							indexs.add(index);
						}
						indexMapper.createIndexs(indexs);//批量创建指标
					}
				}
				if(report.getProjectStatus().equalsIgnoreCase(AuditConstant.AUDIT_COMMIT)){//提交状态
					throw new BusinessException(ExceptionConstant.REPORT_ONLY_ONE_CODE,ExceptionConstant.REPORT_ONLY_ONE_NAME);
				}
			}
				return id;
	}

	/**
	 * 根据实施期目标编号查询实施期目标
	 * @param id
	 * @return 实施期目标类型实体
	 */
	public TargetsOfImplPhaseEntity retrieveTargetsOfImplPhaseById(String id) {
		if(StringUtil.isBlank(id)){
			throw new  BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		}
		return targetsOfImplPhaseMapper.retrieveTargetsOfImplPhase(id);
	}

	/**
	 * 根据实施期目标类的属性查询实施期目标（根据reportId查询实施期目标集合的查询）
	 * @param targetsOfImplPhaseEntity
	 * @return 实施期目标类型集合
	 */
	public List<TargetsOfImplPhaseEntity> retrieveTargetsOfImplPhases(
			TargetsOfImplPhaseEntity targetsOfImplPhaseEntity) {
		return targetsOfImplPhaseMapper.retrieveTargetsOfImplPhases(targetsOfImplPhaseEntity);
	}

	/**
	 * 更新实施期目标 
	 * 先更新实施期目标，以及删除绑定在它下的年度目标，指标，
	 * 再创批量创建年度目标，批量创建指标
	 * @param targetsOfImplPhaseEntity 实施期目标实体
	 * @return true或false
	 */
	public void updateTargetsOfImplPhase(@Valid TargetsOfImplPhaseEntity targetsOfImplPhaseEntity) throws BusinessException{
		if(targetsOfImplPhaseEntity==null){
			throw new  BusinessException(ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE);
		}
		if(targetsOfImplPhaseEntity.getId()==null){
			throw new  BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		}
		/*先删除删除实施期目标，以及绑定在它下的年度目标，指标*/
		targetsOfImplPhaseMapper.updateTargetsOfImplPhase(targetsOfImplPhaseEntity);
		indexMapper.deleteIndexByTid(targetsOfImplPhaseEntity.getId());
		annualTargetMapper.deleteAnnualTargetsByTid(targetsOfImplPhaseEntity.getId());
		/*再创建一个新的的实施期目标，批量创建年度目标，批量创建指标*/
		List<AnnualTargetEntity> annualTargetEntitys=targetsOfImplPhaseEntity.getAnnualTargetDatalist();
		List<AnnualTargetEntity> anns=new ArrayList<AnnualTargetEntity>();
		List<IndexEntity> indexs=new ArrayList<IndexEntity>();
		if(annualTargetEntitys!=null&&annualTargetEntitys.size()>0){
			for(int i=0;i<annualTargetEntitys.size();i++){
				AnnualTargetEntity annT = annualTargetEntitys.get(i);
				annT.setId(UUID.randomUUID().toString());
				annT.setTargetsOfImplPhase(targetsOfImplPhaseEntity.getId());
				anns.add(annT);
			}
			annualTargetMapper.createAnnualTargets(anns);//批量创建年度目标
		}
		List<IndexEntity> indexEntitys=targetsOfImplPhaseEntity.getIndexEntityList();
		if(indexEntitys!=null&&indexEntitys.size()>0){
			for(int i=0;i<indexEntitys.size();i++){
				IndexEntity index = indexEntitys.get(i);
				index.setId(UUID.randomUUID().toString());
				index.setTargetsOfImplPhase(targetsOfImplPhaseEntity.getId());
				indexs.add(index);
			}
			indexMapper.createIndexs(indexs);//批量创建指标
		}		
	}

	/**
	 * 删除实施期目标(根据前台传来的id来删除)
	 * @param id 实施期目标id
	 * @return true或false
	 */
	public void deleteTargetsOfImplPhaseById(String id) throws BusinessException{
		if(StringUtil.isBlank(id)){
			throw new  BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		}
		indexMapper.deleteIndexByTid(id);
		annualTargetMapper.deleteAnnualTargetsByTid(id);
		targetsOfImplPhaseMapper.deleteTargetsOfImplPhase(id);
	}

	/**
	 * 根据审核表编号查询实施期目标
	 * @param reportId
	 * @return 实施期目标集合
	 */
	public List<TargetsOfImplPhaseEntity> retrieveTargetsPhaseByReportId(
			String reportId) {
		return targetsOfImplPhaseMapper.retrieveTargetsPhaseByReportId(reportId);
	}
    
	/**
	 * 根据组织机构id和行政区划编码查询出司局下面所挂省的实施期目标
	 * @param devisionCode 行政区划代码
	 * @param oid 组织机构代码
	 * @return List<TargetsOfImplPhaseEntity> 实施期目标列表
	 */
	public List<TargetsOfImplPhaseEntity> retrieveTargetsPhaseByDivisionCodeAndOrgId(String divisionCode,String oid){
		return targetsOfImplPhaseMapper.retrieveTargetsPhaseByDivisionCodeAndOrgId(divisionCode,oid);
	}
	
	/**
	 * 批量更新年度目标 
	 * @param targetsOfImplPhaseList
	 */
	public void updateTargetsOfImplPhaseList(List<TargetsOfImplPhaseEntity> targetsOfImplPhaseList){
		if(targetsOfImplPhaseList.size()==0){
			throw new BusinessException("000000","集合为空");
		}
		targetsOfImplPhaseMapper.updateTargetsOfImplPhaseList(targetsOfImplPhaseList);
	}
}
